package cc.ufinity.networkdiagnose.widget;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import cc.ufinity.networkdiagnose.manager.Account;
import cc.ufinity.networkdiagnose.manager.ChinaNetAccount;
import cc.ufinity.networkdiagnose.manager.ChinaUnicomAccount;
import cc.ufinity.networkdiagnose.manager.CreateResultXML;
import cc.ufinity.networkdiagnose.manager.DBManager;
import cc.ufinity.networkdiagnose.util.StringUtils;
import cc.ufinity.networkdiagnose.view.R;

public class AccountConfigWidget extends GeneralTestWidget {
	private Button backBtn;
	private Button okBtn;
	private Activity ctx;
	private boolean iscmcc=false,ischinaNet=false,ischinaUnicom=false;
	private EditText cmcc_UserText;
	private EditText cmcc_PwdText;
	private EditText chinaNet_UserText;
	private EditText chinaNet_PwdText;
	private EditText chinaUnicom_UserText;
	private EditText chinaUnicom_PwdText;
	private Spinner chinaNetSpinner;
	private Spinner chinaUnicomSpinner;
	private ArrayAdapter<String> adapter1;
	private ArrayAdapter<String> adapter2;
	 
	private static final String [] countriesStr1 = {
		"安徽","北京","全国中心","天津","河北","山西", "内蒙古","辽宁", "吉林", "黑龙江","湖北", "湖南", "河南",
		 "江苏","山东","上海","浙江", "福建","江西", "广东","海南","广西", "四川","重庆","贵州","云南","西藏","陕西",
		 "甘肃","青海","宁夏", "新疆","澳门", "全国", "统一账号"
		};
	private static final String [] countriesStr2 = {
		"安徽","北京","天津","河北","山西", "内蒙古","辽宁", "吉林", "黑龙江","湖北", "湖南", "河南",
		 "江苏","山东","上海","浙江", "福建","江西", "广东","海南","广西", "四川","重庆","贵州","云南","西藏","陕西",
		 "甘肃","青海","宁夏", "新疆"
		};
	private Map<String, String> map1 = new HashMap<String, String>();
	private Map<String, String> map = new HashMap<String, String>();
	private Account config = new Account();
	private ChinaNetAccount chinaNet=new ChinaNetAccount();
	private ChinaUnicomAccount chinaUnicom=new ChinaUnicomAccount();
	
	public AccountConfigWidget(Activity context) {
		super(context);
		inflate(context, R.layout.account_config_widget, this);
		this.ctx = context;
		
		/*DBManager db = new DBManager(ctx);
		chinaNet = db.getChinaNetAccount();
		db.release();
		if(chinaNet == null) {
			chinaNet=new ChinaNetAccount();
		}
		
		db = new DBManager(ctx);
		chinaUnicom = db.getUnicomAccount();
		db.release();
		if(chinaNet == null) {
			chinaUnicom=new ChinaUnicomAccount();
		}*/
		
		chinaNetSpinner=(Spinner)findViewById(R.id.mySpinner1);
		chinaUnicomSpinner=(Spinner)findViewById(R.id.mySpinner);
		init1();
		init2();
		
		/*chinaNet.address=map.get("安徽");
		chinaUnicom.address=map1.get("安徽");*/
		adapter1 = new ArrayAdapter<String>(ctx,android.R.layout.simple_spinner_dropdown_item,countriesStr1);
		adapter2 = new ArrayAdapter<String>(ctx,android.R.layout.simple_spinner_dropdown_item,countriesStr2);
		chinaNetSpinner.setAdapter(adapter1);
		chinaNetSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view,
			int position, long id) {
				CreateResultXML.a = position;
				System.out.println("+++++++++++"+chinaNetSpinner.getSelectedItem().toString());
				chinaNet.address = map.get(chinaNetSpinner.getSelectedItem().toString());
				System.out.println("中国电信账号配置已保存 点击 ------->  " + chinaNet.address);
			}
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}});
		chinaUnicomSpinner.setAdapter(adapter2);
		chinaUnicomSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view,
			int position, long id) {
				CreateResultXML.b=position;
				chinaUnicom.address=map1.get(chinaUnicomSpinner.getSelectedItem().toString());
				System.out.println("++++++"+chinaUnicom.address);
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}});
		backBtn = (Button) this.findViewById(R.id.account_config_btnback);
		cmcc_UserText = (EditText) this
				.findViewById(R.id.cmcc_accountUser_text);
		cmcc_PwdText = (EditText) this.findViewById(R.id.cmcc_acountPwd_text);
		chinaNet_UserText = (EditText) findViewById(R.id.chinanet_accountUser_text);
		chinaNet_PwdText = (EditText) findViewById(R.id.chinanet_acountPwd_text);
		chinaUnicom_UserText = (EditText) findViewById(R.id.chinaunicom_accountUser_text);
		chinaUnicom_PwdText = (EditText) findViewById(R.id.chinaunicom_acountPwd_text);
		okBtn = (Button) this.findViewById(R.id.accountBtnOk);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				backToConfig(ctx);
			}
		});
		okBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				saveConfig();
				sava();
			}
		});
		init();
	}
	
	public void init1(){
		map.put("安徽", "ah");
        map.put("北京", "bj" );
		map.put("全国中心","zx" );
        map.put("天津", "tj" );
        map.put("河北", "he" );
        map.put("山西", "sx");
        map.put("内蒙古", "nm");
        map.put("辽宁", "ln" );
        map.put("吉林", "jl");
        map.put("黑龙江", "hl");
        map.put("湖北", "hb");
        map.put("湖南", "hn");
        map.put("河南", "ha");
        map.put("江苏", "js");
        map.put("山东", "sd");
        map.put("上海", "sh");
        map.put("浙江", "zj");
        map.put("福建", "fj");
        map.put("江西", "jx");
        map.put("广东", "gd");
        map.put("海南", "hi");
        map.put("广西", "gx");
        map.put("四川", "sc");
        map.put("重庆", "cq");
        map.put("贵州", "gz");
        map.put("云南", "yn");
        map.put("西藏", "xz");
        map.put("陕西", "sn");
        map.put("甘肃", "gs");
        map.put("青海", "gh");
        map.put("宁夏", "nx");
        map.put("新疆", "xj");
        map.put("澳门", "am");
        map.put("全国", "qg");
        map.put("统一账号", "udb");
	}
public void init2(){
    map1.put("安徽", "@wlan.ah.chinaunicom.cn");
	map1.put("北京", "@wlan.bj.chinaunicom.cn" );
    map1.put("天津", "@wlan.tj.chinaunicom.cn" );
    map1.put("河北", "@wlan.he.chinaunicom.cn" );
    map1.put("山西", "@wlan.sx.chinaunicom.cn");
    map1.put("内蒙古", "@wlan.nm.chinaunicom.cn");
    map1.put("辽宁", "@wlan.ln.chinaunicom.cn" );
    map1.put("吉林", "@wlan.jl.chinaunicom.cn");
    map1.put("黑龙江", "@wlan.hl.chinaunicom.cn");
    map1.put("湖北", "@wlan.hb.chinaunicom.cn");
    map1.put("湖南", "@wlan.hn.chinaunicom.cn");
    map1.put("河南", "@wlan.ha.chinaunicom.cn");
    map1.put("江苏", "@wlan.js.chinaunicom.cn");
    map1.put("山东", "@wlan.sd.chinaunicom.cn");
    map1.put("上海", "@wlan.sh.chinaunicom.cn");
    map1.put("浙江", "@wlan.zj.chinaunicom.cn");
    map1.put("福建", "@wlan.fj.chinaunicom.cn");
    map1.put("江西", "@wlan.jx.chinaunicom.cn");
    map1.put("广东", "@wlan.gd.chinaunicom.cn");
    map1.put("海南", "@wlan.hi.chinaunicom.cn");
    map1.put("广西", "@wlan.gx.chinaunicom.cn");
    map1.put("四川", "@wlan.sc.chinaunicom.cn");
    map1.put("重庆", "@wlan.cq.chinaunicom.cn");
    map1.put("贵州", "@wlan.gz.chinaunicom.cn");
    map1.put("云南", "@wlan.yn.chinaunicom.cn");
    map1.put("西藏", "@wlan.xz.chinaunicom.cn");
    map1.put("陕西", "@wlan.sn.chinaunicom.cn");
    map1.put("甘肃", "@wlan.gs.chinaunicom.cn");
    map1.put("青海", "@wlan.qh.chinaunicom.cn");
    map1.put("宁夏", "@wlan.nx.chinaunicom.cn");
    map1.put("新疆", "@wlan.xj.chinaunicom.cn");
}
	private void init() {
		DBManager db = new DBManager(ctx);
		Account config = db.getAccount();
		ChinaNetAccount chinaConfig=db.getChinaNetAccount();
		ChinaUnicomAccount chinaUnicomConfig=db.getUnicomAccount();
		if (null != config) {
			try {
				
			} catch(Exception e) {
			}
			cmcc_UserText.setText(config.userName);
			cmcc_PwdText.setText(config.password);
		}
		if(chinaConfig!=null){
			chinaNet_UserText.setText(chinaConfig.userName);
			chinaNet_PwdText.setText(chinaConfig.password);
			//chinaNetSpinner.setSelection(CreateResultXML.a);
			chinaNetSpinner.setSelection(getChinaNetPosition(chinaConfig.address));
		}
		if(chinaUnicomConfig!=null){
			chinaUnicom_UserText.setText(chinaUnicomConfig.userName);
			chinaUnicom_PwdText.setText(chinaUnicomConfig.password);
			chinaUnicomSpinner.setSelection(getChinaUnicomPosition(chinaUnicomConfig.address));
		}
		db.release();
	}

	private void saveConfig() {
		/** 这个是中国移动的账号配置信息 */
		String cmcc_user = cmcc_UserText.getText().toString();
		String cmcc_pwd = cmcc_PwdText.getText().toString();
		/** 这个是中国电信的账号配置信息 */
		String chinaNetUser = chinaNet_UserText.getText().toString();
		String chinaNetPwd = chinaNet_PwdText.getText().toString();
		/** 这个是中国联通的账号配置信息 */
		String chinaUnicomUser = chinaUnicom_UserText.getText().toString();
		String chinaUnicomPwd = chinaUnicom_PwdText.getText().toString();

		if (StringUtils.isEmpty(cmcc_user) || StringUtils.isEmpty(cmcc_pwd)) {
			this.showMessage(ctx, "请输入中国移动完整的账号信息.");
			iscmcc=false;
		} else {
			iscmcc=true;
			config.userName = cmcc_user;
			config.password = cmcc_pwd;
		}

		if (StringUtils.isEmpty(chinaNetUser)
				|| StringUtils.isEmpty(chinaNetPwd)) {
			this.showMessage(ctx, "请输入中国电信完整的账号信息.");
			ischinaNet=false;
			 
		} else {
			ischinaNet=true;
			chinaNet.userName = chinaNetUser ;
			chinaNet.password = chinaNetPwd ;
			 
		}

		if (StringUtils.isEmpty(chinaUnicomUser)
				|| StringUtils.isEmpty(chinaUnicomPwd)) {
			this.showMessage(ctx, "请输入中国联通完整的账号信息.");
			ischinaUnicom=false;
			 
		} else {
			ischinaUnicom=true;
			chinaUnicom.userName=  chinaUnicomUser;
			chinaUnicom.password =  chinaUnicomPwd;
		}
	}
	public void sava(){
		DBManager db = new DBManager(ctx);
		boolean b = false;
		if(iscmcc){
			if (db.saveAccount(config)) {
				b = true;
				this.showMessage(ctx, " 中国移动账号配置已保存.");
			} else {
				this.showMessage(ctx, "中国移动 账号配置保存出错.");
			}
		}
		if(ischinaNet){
			if (db.saveChinaNetAccount(chinaNet)) {
				b = true;
				System.out.println("中国电信账号配置已保存 ------->  " + chinaNet.address);
				this.showMessage(ctx, " 中国电信账号配置已保存.");
			} else {
				this.showMessage(ctx, "中国电信 账号配置保存出错.");
			}
		}
		if(ischinaUnicom){
			if (db.saveUnicomAccount(chinaUnicom)) {
				b = true;
				this.showMessage(ctx, " 中国联通账号配置已保存.");
			} else {
				this.showMessage(ctx, "中国联通 账号配置保存出错.");
			}
		}
		
		db.release();
		if(b) {
			backToConfig(ctx);
		}
	}
	
	private void showMessage(Activity ctx, String msg) {
		Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
	}
	
	private int getChinaNetPosition(String value) {
		Map<String, String> map = new HashMap<String, String>();

        map.put("ah", "0");
        map.put("bj", "1");
		map.put("zx", "2");
        map.put("tj", "3");
        map.put("he", "4");
        map.put("sx", "5");
        map.put("nm", "6");
        map.put("ln", "7");
        map.put("jl", "8");
        map.put("hl", "9");
        map.put("hb", "10");
        map.put("hn", "11");
        map.put("ha", "12");
        map.put("js", "13");
        map.put("sd", "14");
        map.put("sh", "15");
        map.put("zj", "16");
        map.put("fj", "17");
        map.put("jx", "18");
        map.put("gd", "19");
        map.put("hi", "20");
        map.put("gx", "21");
        map.put("sc", "22");
        map.put("cq", "23");
        map.put("gz", "24");
        map.put("yn", "25");
        map.put("xz", "26");
        map.put("sn", "27");
        map.put("gs", "28");
        map.put("gh", "29");
        map.put("nx", "30");
        map.put("xj", "31");
        map.put("am", "32");
        map.put("qg", "33");
        map.put("udb", "34");
        int i = 0;
        try {
        	i = Integer.parseInt(map.get(value));
        } catch(Exception e) { }
		System.out.println(i + "  ----   电信开户地   ---  " + value);
        return i;
	}
	
	private int getChinaUnicomPosition(String value) {
		Map<String, String> map1 = new HashMap<String, String>();

		map1.put("@wlan.ah.chinaunicom.cn", "0");
		map1.put("@wlan.bj.chinaunicom.cn", "1");
		map1.put("@wlan.tj.chinaunicom.cn", "2");
		map1.put("@wlan.he.chinaunicom.cn", "3");
		map1.put("@wlan.sx.chinaunicom.cn", "4");
		map1.put("@wlan.nm.chinaunicom.cn", "5");
		map1.put("@wlan.ln.chinaunicom.cn", "6");
		map1.put("@wlan.jl.chinaunicom.cn", "7");
		map1.put("@wlan.hl.chinaunicom.cn", "8");
		map1.put("@wlan.hb.chinaunicom.cn", "9");
		map1.put("@wlan.hn.chinaunicom.cn", "10");
		map1.put("@wlan.ha.chinaunicom.cn", "11");
		map1.put("@wlan.js.chinaunicom.cn", "12");
		map1.put("@wlan.sd.chinaunicom.cn", "13");
		map1.put("@wlan.sh.chinaunicom.cn", "14");
		map1.put("@wlan.zj.chinaunicom.cn", "15");
		map1.put("@wlan.fj.chinaunicom.cn", "16");
		map1.put("@wlan.jx.chinaunicom.cn", "17");
		map1.put("@wlan.gd.chinaunicom.cn", "18");
		map1.put("@wlan.hi.chinaunicom.cn", "19");
		map1.put("@wlan.gx.chinaunicom.cn", "20");
		map1.put("@wlan.sc.chinaunicom.cn", "21");
		map1.put("@wlan.cq.chinaunicom.cn", "22");
		map1.put("@wlan.gz.chinaunicom.cn", "23");
		map1.put("@wlan.yn.chinaunicom.cn", "24");
		map1.put("@wlan.xz.chinaunicom.cn", "25");
		map1.put("@wlan.sn.chinaunicom.cn", "26");
		map1.put("@wlan.gs.chinaunicom.cn", "27");
		map1.put("@wlan.qh.chinaunicom.cn", "28");
		map1.put("@wlan.nx.chinaunicom.cn", "29");
		map1.put("@wlan.xj.chinaunicom.cn", "30");
        int i = 0;
        try {
        	i = Integer.parseInt(map1.get(value));
        } catch(Exception e) { }
		System.out.println(i + "  ----   联通开户地   ---  " + value);
        return i;
	}
}
